<?php

namespace app\merchant\controller\store;

use app\merchant\controller\Base;
use app\merchant\model\store\Letter as LetterModel;


/**
 * 管理员站内信类
 * Class StoreUser
 * @package app\merchant\controller\store
 */
class Letter extends Base{


    /**
     * 站内信列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {

        $model = new LetterModel;
        $list = $model->getList($this->request->param());
        return $this->renderSuccess(compact('list'));
    }


    /**
     * 站内信列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function detail()
    {
        $param =  $this->request->param();
        if(!isset($param['id']) || isInt($param['id'])){
            return $this->renderError([],'参数错误');
        }
        $model = LetterModel::detail($param['id']);
        if(!$model){
            return $this->renderError([],"站内信不存在");
        }
        $model->setSee();
        return $this->renderSuccess(compact('model'),'获取成功');
    }



    /**
     * 删除站内信
     * @param $user_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function delete()
    {
        $param =  $this->request->param();
        if(!isset($param['id']) || isInt($param['id'])){
            return $this->renderError([],'参数错误');
        }
        $model = LetterModel::detail($param['id']);

        if(!$model){
            return $this->renderError([],"站内信不存在");
        }
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }
}